package com.casic.modules.sys.controller;

import com.casic.common.utils.R;
import com.casic.common.utils.UploadFileUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/fileTransfer", method = RequestMethod.POST)
public class FileTransferControll {
	@Autowired
	private UploadFileUtil uploadFileUtil;

	// 文件上传
	@ApiOperation(value = "文件上传")
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public R upload(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		List<String> paths = null;
		paths = UploadFileUtil.uploadImage(request);
		for(String path : paths) {
			String relativePath = uploadFileUtil.uploadFileFTP(path);
			String absolutePath = uploadFileUtil.getImagePath(relativePath);
			paths.remove(path);
			paths.add(absolutePath);
		}
		map.put("path", paths);
		return R.ok(map);
	}
}
